package com.example.service;

import cn.hutool.core.date.DateUtil;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Course;
import com.example.entity.Homework;
import com.example.entity.Student;
import com.example.entity.Teacher;
import com.example.mapper.CourseMapper;
import com.example.mapper.HomeworkMapper;
import com.example.mapper.TeacherMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 作业信息表业务处理
 **/
@Service
public class HomeworkService {

    @Resource
    private HomeworkMapper homeworkMapper;
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private TeacherMapper teacherMapper;

    /**
     * 新增
     */
    public void add(Homework homework) {
        Course course = courseMapper.selectById(homework.getCourseId());
        Teacher teacher = teacherMapper.selectById(course.getTeacherId());
        homework.setTeacherId(teacher.getId());
        homeworkMapper.insert(homework);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        homeworkMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        // 使用 Java 21 增强的 Stream API
        ids.forEach(homeworkMapper::deleteById);
    }
    /**
     * 修改
     */
    public void updateById(Homework homework) {
        homeworkMapper.updateById(homework);
    }

    /**
     * 根据ID查询
     */
    public Homework selectById(Integer id) {
        return homeworkMapper.selectById(id);
    }

    /**
     * 查询所有
     * 添加权限控制：学生只能查看自己的作业，教师只能查看自己课程的作业
     */
    public List<Homework> selectAll(Homework homework) {
        List<Homework> list = homeworkMapper.selectAll(homework);
        
        // 获取当前登录用户
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser == null) {
            return list;
        }
        
        // 根据角色过滤作业数据
        return switch (currentUser.getRole()) {
            case "STUDENT" -> {
                // 学生只能看到自己的作业
                Student student = (Student) currentUser;
                yield list.stream()
                        .filter(h -> h.getStudentId().equals(student.getId()))
                        .collect(Collectors.toList());
            }
            case "TEACHER" -> {
                // 教师只能看到自己课程的作业
                Teacher teacher = (Teacher) currentUser;
                yield list.stream()
                        .filter(h -> h.getTeacherId().equals(teacher.getId()))
                        .collect(Collectors.toList());
            }
            default -> list; // 管理员可以看到所有作业
        };
    }

    /**
     * 分页查询
     * 添加权限控制：学生只能查看自己的作业，教师只能查看自己课程的作业
     */
    public PageInfo<Homework> selectPage(Homework homework, Integer pageNum, Integer pageSize) {
        // 获取当前登录用户
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser != null) {
            switch (currentUser.getRole()) {
                case "STUDENT" -> {
                    // 学生只能看到自己的作业
                    Student student = (Student) currentUser;
                    homework.setStudentId(student.getId());
                }
                case "TEACHER" -> {
                    // 教师只能看到自己课程的作业
                    Teacher teacher = (Teacher) currentUser;
                    homework.setTeacherId(teacher.getId());
                }
                // 管理员不做限制
            }
        }
        
        PageHelper.startPage(pageNum, pageSize);
        List<Homework> list = homeworkMapper.selectAll(homework);
        return PageInfo.of(list);
    }

}
